<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"
          name="viewport">
    <link rel="stylesheet" href="/asset/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header"></div>
                <div class="layui-card-body">

                    <!--顶部搜索框 start-->
                    <div class="layui-form">
                        <div class="layui-inline">
                            <label class="layui-form-label">租户名</label>
                            <div class="layui-input-block">
                                <input type="text" placeholder="请输入" autocomplete="off" class="layui-input" name='name' data-search="true">
                            </div>
                        </div>
                    </div>
                    <!--顶部搜索框 end-->

                    <!--顶部按钮-->
                    <div style="padding:10px;text-align: center">
                        <button class="layui-btn layui-btn-radius" data-type="search">
                            <i class="layui-icon layui-icon-search "></i>搜索
                        </button>
                        <button class="layui-btn layui-btn-radius layui-btn-warm" data-type="clear">
                            <i class="layui-icon layui-icon-close "></i>清空
                        </button>
                        <button class="layui-btn layui-btn-radius layui-btn-normal" data-type="save">
                            <i class="layui-icon layui-icon-addition "></i>新增
                        </button>
                        <button class="layui-btn layui-btn-radius layui-btn-danger" data-type="delAll">
                            <i class="layui-icon layui-icon-delete "></i>批量删除
                        </button>
                    </div>

                    <!--表格主体-->
                    <table id="table" lay-filter="table"></table>

                    <!--表格行基本操作模板-->
                    <script type="text/html" id="operation">
                        <a class="layui-btn layui-btn-xs" lay-event="get"><i class="layui-icon layui-icon-survey">查看</i></a>
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update"><i class="layui-icon layui-icon-edit">更新</i></a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete">删除</i></a>
                    </script>

                </div>
            </div>
        </div>
    </div>
</div>

<script src="/asset/layui/layui.js"></script>
<script src="/my/fast.js"></script>
<script>
    layui.use(() => {
        let $ = layui.$
        let table = layui.table;

        // 接口地址
        let page = UPMS_ADDRESS + '/sysTenant/page';
        let del = UPMS_ADDRESS + '/sysTenant/delete';
        let toSaveOrUpdate = './saveOrUpdate.html?id=';

        table.render({
            elem: 'table'
            , url: page
            , method: 'post'
            , height: 'full-190'
            , cellMinWidth: 120
            , cols: [[
                {type: 'numbers'}
                , {type: 'checkbox'}
                , {field: 'id', title: 'ID', align: 'center', sort: true, hide: true}
                , {field: 'name', title: '租户名', align: 'center', sort: true}
                , {title: '操作', align: 'center', toolbar: '#operation'}
            ]]
            , page: true
            , toolbar: true
            , parseData: rs => { // 将原始数据解析成 table 组件所规定的数据
                return {
                    "code": rs.code,
                    "msg": rs.msg,
                    "count": rs.data.total,
                    "data": rs.data.records
                }
            }
            , response: {
                statusCode: 200 // 重新设定成功状态码，默认：0
            }
            , request: { // 重新设定分页参数
                pageName: 'current' // 页码参数名，默认：page
                , limitName: 'size' // 每页显示条数参数名，默认：limit
            }
            , even: true // 开启隔行背景
            , escape: true // 开启 xss 字符过滤
        });

        /**
         * 自定义Layui--基础操作
         */
        function customLayuiBase() {
            let tableId = 'table';
            let tableFilter = 'table';
            let searches = $('[data-search=true]');

            let active = layuiBaseTopBtn($, table, tableId, searches, del, toSaveOrUpdate);
            active.delAll = () => {
                // 得到选中的数据,是一个json数组
                let checkData = table.checkStatus(tableId).data;
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                let ids = [];
                $.each(checkData, (i, v) => {
                    ids.push(v.id);
                });
                this.layuiOpenConfirm('真的删除么？这会清空本系统中该租户的所有数据！', () => {
                    layer.prompt({title: '请输入"我确定"'}, function (value, index, elem) {
                        if (value === '') {
                            return elem.focus();
                        }
                        if (value === '我确定') {
                            postForm(del, {ids: ids.toString()}).then(rs => {
                                layer.close(index);
                                layer.msg('已删除');
                                table.reload(tableId);
                                searches[0].focus();
                            });
                        } else {
                            layer.msg('请输入正确内容');
                        }
                    });
                });
            }
            // 给按钮注册点击事件
            $('.layui-btn').click(function () {
                let type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            // 当按下回车键时，触发搜索
            searches.keydown(e => {
                if (e.keyCode === 13) {
                    $('button[data-type=search]').click();
                }
            });
            // 监听表格行工具栏
            table.on('tool(' + tableFilter + ')', obj => {
                let data = obj.data;
                // 单个查看
                if (obj.event === 'get') {
                    this.layuiOpenFixSizeSubmitWindow('查看', toSaveOrUpdate + data.id, '', true);
                }
                // 单个更新
                if (obj.event === 'update') {
                    this.layuiOpenFixSizeSubmitWindow('更新', toSaveOrUpdate + data.id, '#update', true);
                }
                // 单个删除
                if (obj.event === 'del') {
                    this.layuiOpenConfirm('真的删除么？这会清空本系统中该租户的所有数据！', () => {
                        layer.prompt({title: '请输入"我确定"'}, function (value, index, elem) {
                            if (value === '') {
                                return elem.focus();
                            }
                            if (value === '我确定') {
                                postForm(del, {ids: data.id}).then(rs => {
                                    layer.close(index);
                                    layer.msg('已删除');
                                    table.reload(tableId);
                                    searches[0].focus();
                                });
                            } else {
                                layer.msg('请输入正确内容');
                            }
                        });
                    });
                }
            });
        }

        customLayuiBase();

    });
</script>
</body>
</html>
